package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.permission;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.date.DateStyle;
import com.cqhilink.api.common.utils.date.DateUtil;
import com.kingyun.gpsinspection.purificationservice.common.enums.RolesLevelEnum;
import com.kingyun.gpsinspection.purificationservice.common.enums.RolesLevelEnum;
import com.kingyun.gpsinspection.purificationservice.common.enums.TreeIconSkinEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.RoleMenuMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.*;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmMenuService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmRolesMenuService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmRolesService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.permission.JsmSystemPlatformService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by huangxueqian on 2017/1/6/006.
 */

public class JsmRolesMenuServiceImpl implements JsmRolesMenuService {
    @Autowired
    private JsmMenuService menuService ;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private JsmSystemPlatformService systemPlatformService ;
    @Autowired
    private JsmRolesService rolesService;

    /**
     * @param accountRoles 根据人员角色获取菜单
     * @param subSystem    系统编码
     * @return
     * @throws Exception
     */
    @Override
    public List<RoleMenu> getMenuByRole(List<AccountRole> accountRoles, String subSystem) throws Exception {
        List<RoleMenu> roleMenus = new ArrayList<>();
        if(accountRoles!=null && accountRoles.size() > 0){
            Map selectMap = new HashMap<>();
            selectMap.put("subSystem", subSystem);
            selectMap.put("accountRoles", accountRoles);
            List<RoleMenu> rms = roleMenuMapper.selectRoleMenus(selectMap);
            if (!rms.isEmpty()) {
                for (RoleMenu roleMenu : rms) {
                    roleMenus.add(roleMenu);
                }
            }
        }
        return roleMenus;
    }

    /**
     * @param requireMap
     * @return
     * @throws Exception 返回查询异常
     */
    @Override
    public List<Menu> getMenusByRequire(Map requireMap) throws Exception {
        return roleMenuMapper.selectRequire(requireMap);
    }

    /**
     * @param rolesId 角色Id
     * @param list    菜单List
     * @return
     * @throws Exception
     */
    @Override
    public Integer rolesMenuAdd(String rolesId, List<String> list,Employee employee) throws Exception {
        Integer integer = 0;
        /* 判断 */
        if (list != null) {
            String creator =  employee.getEmployeeId();
            // 判断是分配 还是未分配
            for (String menuId : list) {
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleMenuId(UniqueUtil.uuid());
                roleMenu.setRoleId(rolesId);
                roleMenu.setMenuId(menuId);
                roleMenu.setCreator(creator);
                roleMenu.setModifyer(creator);
                roleMenu.setCreatetime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                roleMenu.setModifytime(DateUtil.DateToString(new Date(), DateStyle.YYYY_MM_DD_HH_MM));
                roleMenu.setState(BigDecimal.valueOf(0));
                Map<String, String> seleMap = new HashMap<>();
                seleMap.put("roleId", rolesId);
                seleMap.put("menuId", menuId);
                RoleMenu rm = roleMenuMapper.selectByRoleMenu(seleMap);
                if (rm == null) {
                    Menu menus =    menuService.selectMenu(menuId);
                    if(menus==null){
                        roleMenu.setSubsystem(menuId); // 查询不到菜单情况为子系统情况
                    }else{
                        roleMenu.setSubsystem(menus.getSystemId()); // 设置所属系统 或者平台
                    }
                    integer = integer + roleMenuMapper.insertSelective(roleMenu);
                }
            }
        }
        return integer;
    }

    /**
     * 角色绑定删除
     * @param rolesId 角色Id
     * @param list    菜单List
     */
    @Override
    public Integer rolesMenuDel(String rolesId, List<String> list) throws Exception {
        int resInt = 0 ;
        if(list != null && list.size() > 0){
            for(String menuId : list){
                RoleMenu roleMenu = new RoleMenu();
                roleMenu.setRoleId(rolesId);
                roleMenu.setMenuId(menuId);
                resInt = resInt + roleMenuMapper.deleteByRoleId(roleMenu);
            }
        }
        return resInt ;
    }

    /**
     * @param rolesId 角色Id 分配菜单的权限设置
     * @param menuId  菜单ID
     */
    @Override
    public List<Ztree> isAllotMenuZtree(String rolesId , String menuId) throws Exception {
        List<Ztree> ztreeList = new ArrayList<>();
        // 第一次的时候menuId 为空 ； 其他方式ajax调用则为少数问题
        if(menuId == null){
            List<SystemPlatform> systemPlatforms =  systemPlatformService.selectAllSystemPlatform();
            if(systemPlatforms != null){
                for(SystemPlatform spf : systemPlatforms){
                    Ztree ztree = new Ztree();
                    ztree.setId(spf.getSystemCode());
                    ztree.setIconSkin(TreeIconSkinEnum.ICON_SUBSYSTEM.getDesc());
                    ztree.setName(spf.getSystemPlatformName());
                    ztree.setIsParent(true);
                    ztree.setOpen(true);
                    ztreeList.add(ztree);
                }
            }
        }else {
            boolean isAdmin = false;
            // 0 超级管理员 1管理员  2 普通管理员


            Roles roles = rolesService.rolesSelect(rolesId);
            if (roles != null) {
                    if (roles.getRoleType().intValue() == RolesLevelEnum.ROLES_S.getCode()) {
                        isAdmin = true;
                    }
                    List<Menu> menus = menuService.selectMenuList(rolesId, menuId, isAdmin);
                    if (menus != null) {
                        for (Menu menu : menus) {
                            Ztree zt = new Ztree();
                            zt.setId(menu.getMenuId());
                            zt.setpId(menu.getParentMenuId());
                            zt.setName(menu.getMenuName());
                            zt.setIconSkin(TreeIconSkinEnum.ICON_MENU.getDesc());
                            zt.setIsParent(false);
                            zt.setOpen(true);
                            // 判断当前菜单是否已经分配
                            RoleMenu roleMenu = new RoleMenu();
                            roleMenu.setRoleId(rolesId);
                            roleMenu.setMenuId(menu.getMenuId());
                            RoleMenu isBoolean = roleMenuMapper.isHasRoleMenu(roleMenu);
                            if (isBoolean != null) {
                                zt.setChecked(true);
                            }
                            ztreeList.add(zt);
                        }
                    }
            }
        }
        return ztreeList ;
    }

    /**
     * @param menus 转换成数据问题；
     * @param systemPlatform
     * @return
     */
    private List<Ztree> officeMenuList(List<Menu> menus,SystemPlatform systemPlatform){
        List<Ztree> resZtree = new ArrayList<>();
        Ztree ztree = new Ztree();
        ztree.setId(systemPlatform.getSystemCode());
        ztree.setpId(systemPlatform.getParentPlatformId());
        ztree.setName(systemPlatform.getSystemPlatformName());
        ztree.setIconSkin(TreeIconSkinEnum.ICON_SUBSYSTEM.getDesc());
        ztree.setOpen(true);
        if(menus.size()>0){
            resZtree.add(ztree);
            for(Menu menu : menus){
                Ztree ztrees = new Ztree();
                ztrees.setId(menu.getMenuId());
                ztrees.setpId(menu.getParentMenuId());
                ztrees.setName(menu.getMenuName());
                ztrees.setIconSkin(TreeIconSkinEnum.ICON_MENU.getDesc());
                ztrees.setOpen(true);
                resZtree.add(ztrees);
            }
        }

        return resZtree ;

    }


}
